home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3357 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  97 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: SORTing problem c++
  5. Date: Sun, 28 Jan 96 02:09:32 GMT
  6. Organization: none
  7. Message-ID: <822794972snz@genesis.demon.co.uk>
  8. References: <310AE6D3.6119@pi.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <310AE6D3.6119@pi.net> heggie@pi.net "heggie" writes:
  15.  
  16. >I'm used to normal c
  17. >just recent i switched to c++, now i am stucked
  18. >with a program that did work in c but doesn't
  19. >in c++.
  20. >it is a program that sorts the 'argv' array,
  21. >what can I DO???
  22. >
  23. >
  24. >
  25. >#include <stdio.h>
  26. >#include <stdlib.h>
  27. >#include <string.h>
  28. >
  29. >
  30. >int sort_function(char **a, char **b)
  31. >{
  32. >   return( strcmp(*a, *b));
  33. >}
  34. >
  35. >
  36. >int main(int argc, char **argv)
  37. >{
  38. >   int
  39. >                x;
  40. >
  41. >   qsort(argv, argc, sizeof(char*), sort_function);
  42. >
  43. >   for (x = 0; x < argc; x++)
  44. >         printf("%s\n", argv[x]);
  45. >   return 0;
  46. >}
  47.  
  48. Your program isn't valid C, let alone C++ (the fact that it happened
  49. to work on your particular system is not relevant to this). The most
  50. obvious problem is that qsort() requires a comparison function of the form
  51.  
  52. int sort_function(const void *, const void *);
  53.  
  54. which yours clearly isn't. An ANSI C compiler should have complained about
  55. this, as will a C++ compiler. Instead use something like:
  56.  
  57. static int sort_function(const void *a, const void *b)
  58. {
  59.     return strcmp(*(const char *const *)a, *(const char *const *)b);
  60. }
  61.  
  62. Your other problem is that it is not legal in C to modify the argv array
  63. of pointers, so you need to make a copy of it and sort that.
  64.  
  65.  
  66. int main(int argc, char **argv)
  67. {
  68.    int    x;
  69.  
  70.    if (argc > 0) {
  71.       char **const argp = malloc(argc * sizeof(*argp));
  72.  
  73.       if (argp == NULL)
  74.           exit(EXIT_FAILURE);
  75.  
  76.       memcpy(argp, argv, argc * sizeof(*argp));
  77.  
  78.       qsort(argp, argc, sizeof *argp, sort_function);
  79.  
  80.       for (x = 0; x < argc; x++)
  81.          printf("%s\n", argp[x]);
  82.  
  83.       free(argp);
  84.    }
  85.  
  86.    return 0;
  87. }
  88.  
  89. Note that, like your code, this includes argv[0] in the sort which is the
  90. program name.
  91.  
  92. -- 
  93. -----------------------------------------
  94. Lawrence Kirby | fred@genesis.demon.co.uk
  95. Wilts, England | 70734.126@compuserve.com
  96. -----------------------------------------
  97.